package cz.hackathon;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import cz.hackathon.core.Controller;
import cz.hackathon.rateit.model.Action;
import cz.hackathon.rateit.model.Category;
import cz.hackathon.rateit.model.Item;

public class ItemsActivity extends Activity implements OnItemClickListener {
	
	private Action action;
	private Category category;
	public static ContentAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.item_list);
		
		action = (Action) getIntent().getSerializableExtra("Action");
		category = (Category) getIntent().getSerializableExtra("Category");
		
		ListView contentList = (ListView) findViewById(R.id.list_content);
		
		adapter = new ContentAdapter(getBaseContext(), getContent());
		contentList.setAdapter(adapter);
		
		contentList.setOnItemClickListener(this);
	}
	
	private class ContentAdapter extends ArrayAdapter<Item> {

		public ContentAdapter(Context context, List<Item> objects) {
			super(context, R.layout.item_single, objects);
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			RelativeLayout clientView;

			Item item = getItem(position);

			if (convertView == null) {
				clientView = new RelativeLayout(getContext());
				getLayoutInflater().inflate(R.layout.item_single,
						clientView, true);
			} else {
				clientView = (RelativeLayout) convertView;
			}

			((TextView) clientView.findViewById(R.id.tv_contentItem))
					.setText(item.getName());
			((CheckBox) clientView.findViewById(R.id.chbox_contentItem))
			.setChecked(item.isFinished());

			return clientView;
		}
		
	}
	
	private List<Item> getContent() { // FIXME opravit az se bude parsovat XML
		return Controller.getItemsByCategory(action, category);//action.getItems();		

	}

	public void onItemClick(AdapterView<?> parent, View v, int position,
			long id) {
		Intent intent = new Intent(v.getContext(), RatingActivity.class);
		intent.putExtra("Item", (Item) parent.getItemAtPosition(position));
		startActivity(intent);
	}
	
	@Override
	protected void onResume(){
		super.onResume();
//		category = (Category) getIntent().getSerializableExtra("Category");
//		adapter.notifyDataSetChanged();
		
		

	}

}
